<?php
/**
 * Created by PhpStorm.
 * Script Name: MpFollowBlack.php
 * Create: 2023/3/3 8:50
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;

use plugin\wechat\app\model\MpFollowBlack as BlackM;
use plugin\wechat\app\model\MpFollow as FollowM;
use support\Container;

class MpFollowBlack extends Mp
{
    /**
     * 拉黑用户
     * @param $openid
     * @param null $mp_info
     * @return mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function block($openid, $mp_info = null)
    {
        try {
            if(strpos($openid, ',') !== false){
                $openid = explode(',', $openid);
            }
            !is_array($openid) && $openid = [$openid];
            empty($mp_info) && $mp_info = \request()->mpInfo;
            $app = self::getApp($mp_info);
            $response = $app->user->block($openid);
            if(!empty($response['errcode'])){
                return $response['errmsg'];
            }
            $insert = [];
            foreach ($openid as $item){
                $insert[] = [
                    'openid' => $item,
                    'mp_id' => $mp_info['id'],
                    'company_id' => $mp_info['company_id'],
                ];
            }
            return Container::make(BlackM::class, [])->saveAll($insert);
        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * 取消拉黑用户
     * @param $openid
     * @param null $mp_info
     * @return mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function unblock($openid, $mp_info = null)
    {
        try {
            if(strpos($openid, ',') !== false){
                $openid = explode(',', $openid);
            }

            empty($mp_info) && $mp_info = \request()->mpInfo;
            $app = self::getApp($mp_info);
            $response = $app->user->unblock($openid);
            if(!empty($response['errcode'])){
                return $response['errmsg'];
            }

            return BlackM::where('mp_id', $mp_info['id'])
                ->where('company_id', $mp_info['company_id'])
                ->where('openid', $openid)
                ->delete();
        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * 黑名单列表
     * @param null $begin_openid
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function blackList($begin_openid = null, $mp_info = null)
    {
        $app = self::getApp($mp_info);
        $response = $app->user->blacklist($begin_openid);
        if(!empty($response['errcode'])){
            return $response['errmsg'];
        }
        return $response;
    }
}